package punctuatedEquilibrium;

public class PunctuatedEquilibriumModel {

	private DataCollector			dataCollector;
	private int						numberOfGenerationsSimulated;
	private Phenotype				optimalPhenotype;
	private Population				population;
	private SimulationParameters	simulationParameters;

	public PunctuatedEquilibriumModel(SimulationParameters parameters) {
		simulationParameters = parameters;
		initializeFields();
	}

	public double[][] reportArrayContainingData() {
		return dataCollector.reportDataArray();
	}

	public void runSimulation() {
		while (numberOfGenerationsSimulated < simulationParameters
				.numberOfGenerations()) {
			runSimulationStepAndCollectData();
			numberOfGenerationsSimulated++;
		}
	}

	private void runSimulationStepAndCollectData() {
		simulateEvolutionOfPopulationUnderSelectivePressure();
		collectData();
		randomlyChangeOptimalTraitsBySmallAmount();
	}

	private void simulateEvolutionOfPopulationUnderSelectivePressure() {
		population.simulateOneGeneration(optimalPhenotype);
	}

	private void collectData() {
		dataCollector.collectData();
	}

	private void initializeFields() {
		numberOfGenerationsSimulated = 0;
		population = new Population(simulationParameters);
		optimalPhenotype = PhenotypeFactory.getStandardPhenotype();
		dataCollector = new DataCollector(population, optimalPhenotype);
	}

	private void randomlyChangeOptimalTraitsBySmallAmount() {
		optimalPhenotype.changeWithSmallVariability();
	}

}
